package practice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.concurrent.Future;

import com.google.appengine.api.urlfetch.FetchOptions;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

public class HttpRequest {
	public static String getAllGroups() throws IOException {

		final String REQUEST_URL = "https://crmdnaserver.appspot.com/_ah/api/group/v1/getAll?client=isha";
		URL url = new URL(REQUEST_URL);

		StringBuilder builder = new StringBuilder();
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(
				url.openStream()))) {
			String line;

			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
		}

		return builder.toString();
	}

	public static Future<HTTPResponse> getAllGroupsAsync() throws IOException {

		final String REQUEST_URL = "https://crmdnaserver.appspot.com/_ah/api/group/v1/getAll?client=isha";
		URL url = new URL(REQUEST_URL);

		URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
		Future<HTTPResponse> response = fetcher.fetchAsync(new HTTPRequest(url, HTTPMethod.GET,
				FetchOptions.Builder.doNotFollowRedirects()));
		
		return response;
	}
}
